package com.design.graduation.asdemo.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.design.graduation.asdemo.R;


/**
 * Created by arvin on 2016/1/18 16:46.
 */
public class TipsDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private EnsureOrCancelListener mListener;
    private TextView messageTv;
    private TextView ensure;
    private TextView cancel;
    private String currentMessage;


    public TipsDialog(Context context, EnsureOrCancelListener listener) {
        super(context, R.style.normal_corner_dialog);
        setContentView(R.layout.dialog_tips);
        this.mContext = context;
        this.mListener = listener;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int screenWidth = (int) (metrics.widthPixels * 0.80);
        getWindow().setLayout(screenWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public TipsDialog(Context context, EnsureOrCancelListener listener, String message) {
        this(context, listener);
        currentMessage = message;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initEvent();
        initData();
    }

    private void initView() {
        messageTv = (TextView) getWindow().findViewById(R.id.message);
        ensure = (TextView) getWindow().findViewById(R.id.ensure);
        cancel = (TextView) getWindow().findViewById(R.id.cancel);
    }

    private void initEvent() {
        findViewById(R.id.ensure).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);
    }

    private void initData() {
        if (!TextUtils.isEmpty(currentMessage)) {
            messageTv.setText(currentMessage);
        }
    }

    public void setMessageText(String message) {
        messageTv.setText(message);
    }

    public void setEnsureText(String message) {
        ensure.setText(message);
    }

    public void setCancelText(String message) {
        cancel.setText(message);
    }

    public void setListener(EnsureOrCancelListener ensureOrCancelListener) {
        mListener = ensureOrCancelListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ensure:
                mListener.onEnsureClicked();
                break;
            case R.id.cancel:
                mListener.onCancelClicked();
                break;
        }
        dismiss();
    }

    public interface EnsureOrCancelListener {
        void onEnsureClicked();

        void onCancelClicked();
    }
}
